//
//  RisacTopBarItem.swift
//  Risac
//
//  Created by remy on 2019/3/27.
//  Copyright © 2019 com.Risac. All rights reserved.
//

public class RisacTopBarItem {
    public enum ItemType {
        case image, text, attr, group
    }
    public let type: ItemType
    public private(set) var text: String?
    public private(set) var attr: NSAttributedString?
    public private(set) var image: UIImage?
    public private(set) var group: [RisacTopBarItem] = []
    public internal(set) weak var itemView: UIView?
    public internal(set) weak var sectionView: UIView?
    public var bgColor: UIColor?
    public var textColor: UIColor?
    public var font: UIFont?
    public var inset: UIEdgeInsets = .zero
    public var margin: UIEdgeInsets = .zero
    public var action: UIGestureRecognizer?
    
    private init(_ type: ItemType) {
        self.type = type
    }
    
    public static func text(_ text: String?, _ closure: ((RisacTopBarItem) -> Void)? = nil) -> RisacTopBarItem {
        let item = RisacTopBarItem(.text)
        item.text = text
        closure?(item)
        return item
    }
    
    public static func attr(_ attr: NSAttributedString?, _ closure: ((RisacTopBarItem) -> Void)? = nil) -> RisacTopBarItem {
        let item = RisacTopBarItem(.attr)
        item.attr = attr
        closure?(item)
        return item
    }
    
    public static func image(_ image: UIImage?, _ closure: ((RisacTopBarItem) -> Void)? = nil) -> RisacTopBarItem {
        let item = RisacTopBarItem(.image)
        item.image = image
        closure?(item)
        return item
    }
    
    public static func group(_ group: RisacTopBarItem...) -> RisacTopBarItem {
        let item = RisacTopBarItem(.group)
        item.group = group
        return item
    }
    
    @discardableResult
    public func bgColor(_ color: UIColor) -> RisacTopBarItem {
        self.bgColor = color
        return self
    }
    
    @discardableResult
    public func textColor(_ color: UIColor) -> RisacTopBarItem {
        self.textColor = color
        return self
    }
    
    @discardableResult
    public func font(_ font: UIFont) -> RisacTopBarItem {
        self.font = font
        return self
    }
    
    @discardableResult
    public func inset(_ inset: UIEdgeInsets) -> RisacTopBarItem {
        self.inset = inset
        return self
    }
    
    @discardableResult
    public func margin(_ margin: UIEdgeInsets) -> RisacTopBarItem {
        self.margin = margin
        return self
    }
    
    @discardableResult
    public func action(_ action: UIGestureRecognizer) -> RisacTopBarItem {
        self.action = action
        return self
    }
}
